<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入维保人员名称" style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddMaintainer('add')" v-auth="'BRANCH'">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增人员
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="name" label="姓名" show-overflow-tooltip></el-table-column>
				<el-table-column prop="tel" label="手机号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="branch" label="网点" show-overflow-tooltip></el-table-column>
				<el-table-column prop="dealer" label="经销商" show-overflow-tooltip></el-table-column>
				<el-table-column prop="company" label="厂家" show-overflow-tooltip></el-table-column>
				<el-table-column prop="account" label="账号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="status" label="用户状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag v-if="scope.row.status == 'enable'" type="success">可用</el-tag>
						<el-tag v-if="scope.row.status == 'disable'" type="danger">不可用</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" width="130">
					<template #default="scope">
						<el-button v-auth="'BRANCH'" size="small" text type="primary" @click="onOpenEditMaintainer('edit', scope.row)">修改</el-button>
						<el-button
							v-auth="'BRANCH'"
							size="small"
							text
							type="primary"
							@click="onToggoleMaintainerStatue(scope.row, scope.row.status == 'enable' ? 0 : 1)"
							>{{ scope.row.status === 'enable' ? '禁用' : '启用' }}</el-button
						>
						<el-button v-auth="'BRANCH'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<MaintainerDialog ref="maintainerDialogRef" @refresh="getTableData()" />
		<MaintainerEditDialog ref="maintainerEditDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemUser">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useMaintainer } from './maintainer';
const maintainerStore = useMaintainer();
// 引入组件
const MaintainerDialog = defineAsyncComponent(() => import('/@/views/org/maintainer/dialog.vue'));
const MaintainerEditDialog = defineAsyncComponent(() => import('/@/views/org/maintainer/editDialog.vue'));
// 定义变量内容
const maintainerDialogRef = ref();
const maintainerEditDialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
		},
	},
});

// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	maintainerStore
		.getMaintainerList(name.value, state.tableData.param.pageSize, state.tableData.param.pageNum)
		.then((res) => {
			state.tableData.data = res.list;
			state.tableData.total = res.total;
			state.tableData.loading = false;
		})
		.catch((err) => {
			console.log(err);
			state.tableData.loading = false;
		});
};
// 打开新增用户弹窗
const onOpenAddMaintainer = (type: string) => {
	maintainerDialogRef.value.openDialog(type);
};
// 打开修改用户弹窗
const onOpenEditMaintainer = (type: string, row: RowUserType) => {
	maintainerEditDialogRef.value.openDialog(row);
};

const onToggoleMaintainerStatue = (row, status) => {
	maintainerStore
		.toggoleStatus(row.id, status)
		.then(() => {
			getTableData();
			ElMessage.success('操作成功');
		})
		.catch(() => {
			ElMessage.error('操作失败');
		});
};
// 删除用户
const onRowDel = (row: RowUserType) => {
	ElMessageBox.confirm(`此操作将永久删除账户名称：“${row.name}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			maintainerStore.delete(row.id).then(() => {
				getTableData();
				ElMessage.success('删除成功');
			}).catch;
		})
		.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
./maintainer
